package androidGismeteoWeather;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.*;
import java.lang.Number.*;
import java.text.SimpleDateFormat;

public class SAXPars extends DefaultHandler{
    private String thisElement = null;
    private weatherData data = new weatherData(); 
    int timestep = 0;

    @Override
    public void startDocument() throws SAXException {
        System.out.println("Parsing start");
    }

    @Override
    public void endDocument() throws SAXException {
        System.out.println("Parsing stop");
        System.out.println("\nOutput start");
        System.out.println("-------------");
        System.out.println("City: " + data.getPoint_name());
        SimpleDateFormat sdf = new SimpleDateFormat("d MMMM yyyy 'at' HH:mm");
        System.out.println("Date-time: " + sdf.format(data.getPoint_timestamp()));
        for (int i = 0; i < 3; i++) {
            System.out.println("Forecast for " + data.getTime_step(i) + " hours:");
            System.out.println("    Forecast in " + data.getDatetime(i));
            System.out.println("    Clowdness in %" + data.getCloud_cover(i));
            System.out.println("    Atmospheric pressure: " + data.getPressure(i));
            System.out.println("    Temperature: " + data.getTemperature(i));
            System.out.println("    Wind direction: " + data.getWind_direction(i));
            System.out.println("    Wind velocity: " + data.getWind_velocity(i));
            System.out.println("    Falls: " + data.getFalls(i));
        }
        System.out.println("-----------");
        System.out.println("Output stop");
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
            thisElement = qName;
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        thisElement = "";
    }

    @Override
     public void characters(char[] ch, int start, int length) throws SAXException {
        if (thisElement.equals("point_name")) {
            data.setPoint_name(new String(ch, start, length));
        }
        if (thisElement.equals("point_timestamp")) {
            data.setPoint_timestamp(Integer.parseInt(new String(ch, start, length)));
        }
        if (thisElement.equals("timestep")) {
            timestep++;
        }
        if (thisElement.equals("time_step")) {
            data.setTime_step(timestep - 1, Integer.parseInt(new String(ch, start, length)));
        }
        if (thisElement.equals("datetime")) {
            data.setDatetime(timestep - 1, new String(ch, start, length));
        }
        if (thisElement.equals("cloud_cover")) {
            data.setCloud_cover(timestep - 1, Integer.parseInt(new String(ch, start, length)));
        }
        if (thisElement.equals("pressure")) {
            data.setPressure(timestep - 1, Integer.parseInt(new String(ch, start, length)));
        }
        if (thisElement.equals("temperature")) {
            data.setTemperature(timestep - 1, Integer.parseInt(new String(ch, start, length)));
        }
        if (thisElement.equals("wind_direction")) {
            data.setWind_direction(timestep - 1, new String(ch, start, length));
        }
        if (thisElement.equals("wind_velocity")) {
            data.setWind_velocity(timestep - 1, Integer.parseInt(new String(ch, start, length)));
        }
        if (thisElement.equals("falls")) {
            data.setFalls(timestep - 1, Integer.parseInt(new String(ch, start, length)));
        }
     }

    @Override
    public void warning(SAXParseException e) throws SAXException {
        super.warning(e); 
    }

    @Override
    public void error(SAXParseException e) throws SAXException {
        super.error(e); 
    }

    @Override
    public void fatalError(SAXParseException e) throws SAXException {
        super.fatalError(e); 
    }
}